# 实现注册登录功能
with open('./password.txt', 'r+') as fp:
    type = input('请输入要进行的操作的序号（1.注册 2.登录）：')
    if type == '1':
        username = input('请输入用户名：')
        password = input('请输入密码：')
        allUsername = []
        for i in fp.readlines():
            username = i.split('\n')[0].split('-')[0]
            allUsername.append(username)
        if username in allUsername:
            print('账号已存在！')
        else:
            # 将文件指针指到最后
            fp.seek(0, 2)
            fp.write(username+'-'+password+'\n')
            print('注册成功！')
    else:
        # 将文件指针指到开始
        fp.seek(0)
        account = fp.readlines()
        accountDict = {}
        for i in account:
            itemAccount = i.split('\n')[0]
            username = itemAccount.split('-')[0]
            password = itemAccount.split('-')[1]
            accountDict[username] = password
        inputUsername = input('请输入用户名：')
        inputPassword = input('请输入密码：')
        if not accountDict.get(inputUsername):
            print('账号不存在！')
        elif inputPassword != accountDict[inputUsername]:
            print('密码错误！')
        elif inputPassword == accountDict[inputUsername]:
            print('登录成功！')


